using System.Windows.Controls.Primitives;
using DimensionsHelper.Client.ViewModels.CsvFormatter;

namespace DimensionsHelper.Client.Views.CsvFormatter;

public partial class TableStyleOptionsPage
{
    public TableStyleOptionsPage(IServiceProvider serviceProvider)
    {
        InitializeComponent();
        DataContext = serviceProvider.GetRequiredService<TableStyleOptionsViewModel>();
        _rangeButtons =
        [
            SectionLabelButton,
            HeaderRangeButton,
            SigCharacterLineButton,
            SectionMemberCellButton,
            TitleCellButton,
            BaseLabelCellButton,
            BaseValueRangeButton,
            SideRangeButton,
            InnerTextButton,
            DataRangeButton,
            LowerSigButton,
            UpperSigButton,
        ];
    }


    private readonly ToggleButton[] _rangeButtons;
    private bool _uncheckInternal;


    private void UncheckInternal(ToggleButton button)
    {
        _uncheckInternal = true;
        
        foreach (ToggleButton item in _rangeButtons)
        {
            if (item != button)
            {
                item.IsChecked = false;
            }
        }

        _uncheckInternal = false;
    }


    private void ResetCurrentStyle(object sender, RoutedEventArgs e)
    {
        if (_uncheckInternal)
        {
            return;
        }

        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel;
        viewModel.CurrentFont = viewModel;
    }

    private void SectionLabelButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.SectionStyle;
        viewModel.CurrentFont = viewModel.SectionStyle;
        UncheckInternal(SectionLabelButton);
    }

    private void HeaderRangeButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.HeaderStyle;
        viewModel.CurrentFont = viewModel.HeaderStyle;
        UncheckInternal(HeaderRangeButton);
    }

    private void SigCharacterLineButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.Sig.SigMarkRowStyle;
        viewModel.CurrentFont = viewModel.Sig.SigMarkRowStyle;
        UncheckInternal(SigCharacterLineButton);
    }

    private void SectionMemberCellButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.SectionMemberStyle;
        viewModel.CurrentFont = viewModel.SectionMemberStyle;
        UncheckInternal(SectionMemberCellButton);
    }

    private void TitleCellButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.TitleStyle;
        viewModel.CurrentFont = viewModel.HyperlinkStyle;
        UncheckInternal(TitleCellButton);
    }

    private void BaseLabelCellButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.BaseLabelStyle;
        viewModel.CurrentFont = viewModel.BaseLabelStyle;
        UncheckInternal(BaseLabelCellButton);
    }

    private void BaseValueRangeButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.BaseValueStyle;
        viewModel.CurrentFont = viewModel.BaseValueStyle;
        UncheckInternal(BaseValueRangeButton);
    }

    private void SideRangeButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.SideStyle;
        viewModel.CurrentFont = viewModel.SideStyle;
        UncheckInternal(SideRangeButton);
    }

    private void InnerTextButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.InnerTextStyle;
        viewModel.CurrentFont = viewModel.InnerTextStyle;
        UncheckInternal(InnerTextButton);
    }

    private void DataRangeButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.DataStyle;
        viewModel.CurrentFont = viewModel.DataStyle;
        UncheckInternal(DataRangeButton);
    }

    private void LowerSigButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel;
        viewModel.CurrentFont = viewModel.Sig.LowerCaseStyle;
        UncheckInternal(LowerSigButton);
    }

    private void UpperSigButton_Checked(object sender, RoutedEventArgs e)
    {
        TableStyleOptionsViewModel viewModel = (TableStyleOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel;
        viewModel.CurrentFont = viewModel.Sig.UpperCaseStyle;
        UncheckInternal(UpperSigButton);
    }
    

    private void MergeSigToggleButton_Checked(object sender, RoutedEventArgs e)
    {
        HideSigToggleButton.IsChecked = false;
    }

    private void HideSigToggleButton_Checked(object sender, RoutedEventArgs e)
    {
        MergeSigToggleButton.IsChecked = false;
    }

    private void ShowSectionToggleButton_Checked(object sender, RoutedEventArgs e)
    {
        ReadSectionToggleButton.IsChecked = true;
    }

    private void ReadSectionToggleButton_Unchecked(object sender, RoutedEventArgs e)
    {
        ShowSectionToggleButton.IsChecked = false;
    }
}